<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of myPoolsCooker
 *
 * @author i062261
 */
class BWeEventWePollCooker {

    public function showAddWePoll($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollId = $activeLink->getLinkVariable('pollId');

        $updateControl->addRenderView('addWePoll', 'wePoll_add', array(
            'eventId' => $eventId,
            'pollId' => $pollId,
        ));
    }

    public function showPollItemList($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollId = $activeLink->getLinkVariable('pollId');

        $updateControl->addRenderView('pollItemList', 'wePoll_question_list', array(
            'eventId' => $eventId,
            'pollId' => $pollId,
        ));
    }

    public function showAddPollItem($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollId = $activeLink->getLinkVariable('pollId');
        $pollItemId = $activeLink->getLinkVariable('pollItemId');

        $updateControl->addRenderView('addWePollItem', 'wePoll_question_add', array(
            'eventId' => $eventId,
            'pollId' => $pollId,
            'pollItemId' => $pollItemId
        ));
    }

    public function savePoll($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollId = $activeLink->getLinkVariable('pollId');

        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);

        $pollTable = new BWWePollSheet('wePoll', $event);
        if (is_numeric($pollId) && $pollId > 0) {
            $poll = $pollTable->getListById($pollId);
        } else {
            $poll = $pollTable->getNewList();
        }

        $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'poll_info');

        $itemData = array();
        foreach ($mediaList as $media) {
            $name = $media->name;
            $value = $media->content;
            $itemData[$name] = $value;
        }

        //save to table
        $poll->addItems($itemData);

        //show event list
        $updateControl->addRenderView('wePollListManage', 'wePoll_list_manage', array('eventId' => $eventId));
    }

    public function savePollItem($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $pollId = $activeLink->getLinkVariable('pollId');
        $pollItemId = $activeLink->getLinkVariable('pollItemId');

        $pollTable = new BWWePollSheet('wePoll');
        $poll = $pollTable->getListById($pollId);

        $pollItemTable = new BWWePollSheet('wePollItem', $poll);
        if (is_numeric($pollItemId) && $pollItemId > 0) {
            $pollItem = $pollItemTable->getListById($pollItemId);
        } else {
            $pollItem = $pollItemTable->getNewList();
        }

        $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'pollItem_info');

        $itemData = array();
        foreach ($mediaList as $media) {
            $name = $media->name;
            $value = $media->content;
            $itemData[$name] = $value;
        }

        //save to table
        $pollItem->addItems($itemData);

        //show event list
        $updateControl->addRenderView('wePollListManage', 'wePoll_list_manage', array('eventId' => $eventId));
    }

    public function addPollItemOption($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollItemId = $activeLink->getLinkVariable('pollItemId');
        $pollOptionId = $activeLink->getLinkVariable('pollOptionId');

        $pollItemTable = new BWWePollSheet('pollItem');
        $pollItem = $pollItemTable->getListById($pollItemId);

        $pollOptionTable = new BWWePollSheet('pollOption', $pollItem);
        $optionCount = $pollOptionTable->getCount();

        if ($pollOptionId == 0 || $pollOptionId == NULL) {
            $pollOption = $pollOptionTable->getNewList();
        } else
            $pollOption = $pollOptionTable->getListById($pollOptionId);

        $MediaList = $processPostObj->populateMediaListFromPostData($postData, 'pollItem_option');

        $itemData = array();
        $i = $optionCount + 3; //additional option from option3
        foreach ($mediaList as $media) {
            $name = 'option' . $i;
            $value = $media->content;
            $itemData[$name] = $value;
            $i++;
        }

        //save to table
        $pollOption->addItems($itemData);

        //show event list
        $updateControl->addRenderView('wePoll_add_question', 'wePoll_add_question', array(
            'eventId' => $eventId,
            'pollItemId' => $pollItemId,
        ));
    }

    public function addPollItemAttribute($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollAttributeId = $activeLink->getLinkVariable('pollAttributeId');
        $pollItemId = $activeLink->getLinkVariable('pollItemId');
        $pollId = $activeLink->getLinkVariable('pollId');

        $pollItemTable = new BWWePollSheet('pollItem');
        $pollItem = $pollItemTable->getListById($pollItemId);

        $pollAttributeTable = new BWWePollSheet('pollAttribute', $pollItem);

        if ($pollAttributeId == 0 || $pollAttributeId == NULL) {
            $pollAttribute = $pollAttributeTable->getNewList();
        } else
            $pollAttribute = $pollAttributeTable->getListById($pollAttributeId);

        $MediaList = $processPostObj->populateMediaListFromPostData($postData, 'pollItem_attribute');

        $itemData = array();
        foreach ($mediaList as $media) {
            $name = $media->name;
            $value = $media->content;
            $itemData[$name] = $value;
        }

        //save to table
        $pollAttribute->addItems($itemData);

        //show event list
        $updateControl->addRenderView('wePoll_add_question', 'wePoll_add_question', array(
            'pollId' => $pollId,
            'pollItemId' => $pollItemId,
        ));
    }

    public function showPollNow($data = NULL) {
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollId = $activeLink->getLinkVariable('pollId');

        $updateControl->addRenderView('showPollNow', 'wePoll_poll', array('eventId' => $eventId, 'pollId' => $pollId));
    }

    /**
     * this function complete below logic
     * 1)populate all questions and options for poll
     * 2)save user's answer into WePoll_Input table
     * @param type $data
     */
    public function pollNow($data = NULL) {
        $sender = Blyn::app()->getUIManager()->getSenderControl();
        $updateControl = Blyn::app()->getUIManager()->getUpdateControl();
        $screen = Blyn::app()->getUIManager()->getCurrentScreen();
        $processPostObj = new BAProcessData();
        $postData = $processPostObj->parsedModelData();
        $activeLink = Blyn::app()->getUIManager()->getActiveLink();
        $eventId = $activeLink->getLinkVariable('eventId');
        $pollId = $activeLink->getLinkVariable('pollId');

        $eventTable = new BAEventSheet();
        $event = $eventTable->getListById($eventId);

        $pollTable = new BWWePollSheet('WePoll');
        $poll = $pollTable->getListById($pollId);

        $pollItemTable = new BWWePollSheet('WePollItem', $poll);
        $pollItemView = $pollItemTable->getView();

        foreach ($pollItemView as $itemId => $pollItem) {

            $pollValueTable = new BWWePollSheet('WePollValue');
            //get current user
            $pollValueList = $pollValueTable->getList(array('userId' => Blyn::app()->getCurrentUser()->getId(), 'pollItemId' => $itemId));

            if (!$pollValueList) {
                $pollValueList = $pollValueTable->getNewList();
            }

            $question = $pollItem->getItem('question')->getValue();

            $itemData = array();
            $itemData['userId'] = Blyn::app()->getCurrentUser()->getId();
            $itemData['pollItemId'] = $itemId;
            $itemData['question'] = $question;

            $mediaList = $processPostObj->populateMediaListFromPostData($postData, 'pollItem_' . $itemId);
            foreach ($mediaList as $media) {
                $name = $media->name;
                $value = $media->content;
                $optionName = 'option' . $value;
                $optionValue = $pollItem->getItem($optionName)->getValue();
                $itemData['optionName'] = $optionName;
                $itemData['optionValue'] = $optionValue;
            }
            
            //save to db
            $pollValueList->addItems($itemData);
        }
        
        $updateControl->addRenderView('wePoll_question_list', 'wePoll_question_list', array(
            'pollId'=>$pollId,
            'eventId'=>$eventId
        ));
    }

}
